package com.corpgovernment.organization.dataloader;

import com.corpgovernment.api.platform.soa.PayBillClient;
import com.corpgovernment.api.platform.soa.getpaypaymentbill.allData.request.GetAllPayBillDataRequest;
import com.corpgovernment.api.platform.soa.getpaypaymentbill.allData.response.GetAllPayBillDataResponse;
import com.corpgovernment.api.platform.soa.response.GetOrderBillDateResponse;
import com.corpgovernment.common.base.JSONResult;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
@Slf4j
public class PpPaymentBillClientLoader {

    @Autowired
    private PayBillClient payBillClient;

    public Map<String, List<GetAllPayBillDataResponse>> getAllPay (Boolean flag) {
        GetAllPayBillDataRequest request = new GetAllPayBillDataRequest();
        request.setIsGetOldData(flag);

        JSONResult<Map<String, List<GetAllPayBillDataResponse>>> result = payBillClient.getAllPayBillData(request);
        if (result == null || result.getData() == null) {
            log.warn("查询支付单数据异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return Maps.newHashMap();
        }
        return result.getData();

    }

    public List<GetOrderBillDateResponse> getOrderBillData (Boolean flag, List<String> orderId, String orderType, Date staDate, Date endDate) {
        GetAllPayBillDataRequest request = new GetAllPayBillDataRequest();
        request.setIsGetOldData(flag);
        request.setOrderList(orderId);
        request.setOrderType(orderType);
        request.setBeginTime(staDate);
        request.setEndTime(endDate);

        JSONResult<List<GetOrderBillDateResponse>> result = payBillClient.getOrderBillData(request);
        if (result == null || result.getData() == null) {
            log.error("查询支付单数据异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return Lists.newArrayList();
        }
        return result.getData();

    }

    public List<GetOrderBillDateResponse> getOrderBillDataByType (Boolean flag, List<String> orderId, String orderType) {
        GetAllPayBillDataRequest request = new GetAllPayBillDataRequest();
        request.setIsGetOldData(flag);
        request.setOrderType(orderType);
        log.info("参数传递:{}, {}", orderId, JsonUtils.toJsonString(request));
        JSONResult<List<GetOrderBillDateResponse>> result = payBillClient.getOrderBillDataByType(request);
        if (result == null || result.getData() == null) {
            log.error("查询支付单数据异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return Lists.newArrayList();
        }
        return result.getData();

    }
}
